/*
 * Copyright 2011 Sebastian Maurer
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package erepublik.dto;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.xml.bind.DatatypeConverter;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Citizen extends IDNameType {

	private Military military = new Military();
	
	private Party party = new Party();
	
	@XmlElement(name="avatar-link")
	private String avatarLink;
	
	private RegionCountry citizenship;
	
	@XmlElement(name="is-congressman")
	private Boolean congressman;
	
	@XmlJavaTypeAdapter(value=DateAdapter.class)
	@XmlElement(name="date-of-birth")
	private Calendar dateOfBirth;
	
	private Citizen employer;
	
	@XmlElement(name="experience-points")
	private Integer experiencePoints;
	
	@XmlElement(name="is-general-manager")
	private Boolean generalManager;
	
	private Integer happiness;
	
	private Integer level;
	
	@XmlElementWrapper(name="medals")
	@XmlElement(name="medal")
	private List<Medal> medals;
	
	@XmlElementWrapper(name="military-skills")
	@XmlElement(name="military-skill")
	private List<MilitarySkill> militarySkills;
	
	@XmlElement(name="is-organization")
	private Boolean organization;

	@XmlElement(name="is-president")
	private Boolean president;

	private RegionCountry residence;

	private String sex;
	
	private Float wellness;
	
	@XmlElement(name="work-skill-points")
	private Integer workSkillPoints;
	
	public String getAvatarLink() {
		return avatarLink;
	}
	
	public Boolean getCongressman() {
		return congressman;
	}
	
	public Calendar getDateOfBirth() {
		return dateOfBirth;
	}
	
	public Citizen getEmployer() {
		return employer;
	}

	public Integer getExperiencePoints() {
		return experiencePoints;
	}

	public Boolean getGeneralManager() {
		return generalManager;
	}

	public Integer getHappiness() {
		return happiness;
	}

	public Integer getLevel() {
		return level;
	}

	public List<Medal> getMedals() {
		return medals;
	}

	public List<MilitarySkill> getMilitarySkills() {
		return militarySkills;
	}

	public Boolean getOrganization() {
		return organization;
	}

	public Boolean getPresident() {
		return president;
	}

	public RegionCountry getResidence() {
		return residence;
	}

	public String getSex() {
		return sex;
	}

	public Float getWellness() {
		return wellness;
	}

	public void setAvatarLink(String avatarLink) {
		this.avatarLink = avatarLink;
	}

	public void setCongressman(Boolean congressman) {
		this.congressman = congressman;
	}

	public void setDateOfBirth(Calendar dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

	public void setEmployer(Citizen employer) {
		this.employer = employer;
	}

	public void setExperiencePoints(Integer experiencePoints) {
		this.experiencePoints = experiencePoints;
	}

	public void setGeneralManager(Boolean generalManager) {
		this.generalManager = generalManager;
	}

	public void setHappiness(Integer happiness) {
		this.happiness = happiness;
	}

	public void setLevel(Integer level) {
		this.level = level;
	}

	public void setMedals(List<Medal> medals) {
		this.medals = medals;
	}

	public void setMilitarySkills(List<MilitarySkill> militarySkills) {
		this.militarySkills = militarySkills;
	}

	public void setOrganization(Boolean organization) {
		this.organization = organization;
	}

	public void setPresident(Boolean president) {
		this.president = president;
	}

	public void setResidence(RegionCountry residence) {
		this.residence = residence;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public void setWellness(Float wellness) {
		this.wellness = wellness;
	}

	public void setCitizenship(RegionCountry citizenship) {
		this.citizenship = citizenship;
	}

	public RegionCountry getCitizenship() {
		return citizenship;
	}

	public void setWorkSkillPoints(Integer workSkillPoints) {
		this.workSkillPoints = workSkillPoints;
	}

	public Integer getWorkSkillPoints() {
		return workSkillPoints;
	}

	public Integer getFightCount() {
		return military.fightCount;
	}

	public String getRank() {
		return military.rank;
	}

	public Integer getRankPoints() {
		return military.rankPoints;
	}

	public Integer getStars() {
		return military.stars;
	}

	public Integer getTotalDamage() {
		return military.totalDamage;
	}

	public void setFightCount(Integer fightCount) {
		this.military.fightCount = fightCount;
	}

	public void setRank(String rank) {
		this.military.rank = rank;
	}

	public void setRankPoints(Integer rankPoints) {
		this.military.rankPoints = rankPoints;
	}

	public void setStars(Integer stars) {
		this.military.stars = stars;
	}

	public void setTotalDamage(Integer totalDamage) {
		this.military.totalDamage = totalDamage;
	}

	public Integer getPartyId() {
		return party.id;
	}

	public String getPartyName() {
		return party.name;
	}

	public Boolean getPartyPresident() {
		return party.president;
	}

	public void setPartyId(Integer id) {
		this.party.id = id;
	}

	public void setPartyName(String name) {
		this.party.name = name;
	}

	public void setPartyPresident(Boolean president) {
		this.party.president = president;
	}
	
	@XmlAccessorType(XmlAccessType.FIELD)
	private static class Military {
		
		@XmlElement(name="fight-count")
		private Integer fightCount;

		private String rank;
		
		private Integer stars;
		
		@XmlElement(name="total-damage")
		private Integer totalDamage;

		@XmlElement(name="rank-points")
		private Integer rankPoints;
		
	}

	@XmlAccessorType(XmlAccessType.FIELD)
	private static class Party {
		
		private Integer id;
		
		private String name;
		
		private Boolean president;
		
	}
	
	private static class DateAdapter extends XmlAdapter<String, Calendar> {

		private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		@Override
		public String marshal(Calendar date) throws Exception {
			return DatatypeConverter.printDateTime(date);
		}

		@Override
		public Calendar unmarshal(String source) throws Exception {
			try {
				return DatatypeConverter.parseDateTime(source);
			} catch(IllegalArgumentException e) {
				Calendar cal = Calendar.getInstance();
				cal.setTime(FORMAT.parse(source));
				return cal;
			}
		}
		
	}
	
}